package com.chenjian.designpattern.proxy.cglibproxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author TC2952
 */
public class CglibProxy {

    public Object getProxy(Object object) {

        Object o = Enhancer.create(object.getClass(), new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {

                System.out.println("begin");
                Object invoke = method.invoke(object, objects);
                //未抛出异常时候提交事务
                System.out.println("end");
                return invoke;
            }
        });
        return o;
    }

    public static void main(String[] args) {
        CgLibEntity proxy = (CgLibEntity) new CglibProxy().getProxy(new CgLibEntity());
        proxy.show();
    }

}
